<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2018/9/18
 * Time: 18:56
 */
class ctrl_config extends L{

    /**
     * 描述；系统配置的展示页面
     *
     * 作者：Zoro.Zhu
    **/
    public function index(){
        $this->view->_pagingHtml = $this->paging();
        $this->view->headTitle = '系统配置';
        $this->display('/html/config.html');
    }

    /**
     * 描述 : 获取分页
     * 作者 : Zoro.Zhu
     */
    public function &paging($params = array()){
        $sql = $this->getInfoSql($params);
        $config = array(
            '配置标识' => '{`type`}',
            '配置说明' => '{`remark`}',
            '操作人' => '{`operName`}',
            '操作时间' => '{`operTime`}',
            '操作'=> "<button class=\"btn btn-oper btn-primary btn-sm\" type=\"button\"
                                    onclick=\"configObj.getConfigInfo('{`type`}');\">
                                <span class=\"fa fa-plus\"></span>修改配置
                            </button>",
            '_attr' => array(
                "attr" => array (
                    "table" => 'id=config class="table table-hover table-condensed table-striped table-scroll jsHoverDetails textC"',
                    "btr" => "align='center' class='of-paging_item_odd tr_{`type`}'",
                ),
                'data' => &$sql,
                'params' => &$params,
                'call' => array($this, 'asCall'),
                'method' => __METHOD__,
                'save' => 'auto',
                'empty' => '查无此数据'
            )
        );
        return $this->_com->paging($config);
    }

    /**
     * 描述 : 获取查询SQL
     * 作者 : Zoro.Zhu
     */
    public function getInfoSql($params){
        $sql = "SELECT
            `type`, `remark`, `operName`, `operTime`
        FROM `vs_config`
        WHERE 1";
        $sql .= !empty($params['search']['type']) ? " AND `type` = '{$params['search']['type']}' " : "";
        return $sql;
    }

    /**
     * 描述 : 整理数据
     * 作者 : Zoro.Zhu
     */
    public function asCall(&$data){

    }

    /**
     * 描述 : 修改配置信息
     * 作者 : Zoro.Zhu
     */
    public function getConfigInfo(){
        if(!empty($_REQUEST['type'])) {
            $sql = "SELECT
                `type`, `data`, `remark`
            FROM `vs_config`
            WHERE `type` = '{$_REQUEST['type']}'";
            $autoConfig = L::sql($sql);
            if(!empty($autoConfig)){
                $this->view->type = $autoConfig[0]['type'];
                $this->view->data = json_decode($autoConfig[0]['data'], true);
                $this->view->remark = $autoConfig[0]['remark'];
            }
        }
        $this->display('/html/getConfig.html');
    }

    /**
     * 描述 : 修改配置
     * 作者 : Zoro.Zhu
     */
    public function updConfig(){
        if(!empty($_POST['type']) && !empty($_POST['remark'])) {
            $key = !empty($_POST['key']) ? $_POST['key'] : array();
            $value = !empty($_POST['value']) ? $_POST['value'] : array();
            $data = array();
            foreach($key as $k => $v){
                if(!empty($v)){
                    $data[$v] = $value[$k];
                }
            }
            $data = addslashes(json_encode($data));
            L::sql(null);
            $user = 'VS';

            $sql = "`remark` = '{$_POST['remark']}',
            `type` = '{$_POST['type']}',
            `data` = '{$data}',
            `operName` = '{$user}',
            `operTime` = '".date('Y-m-d H:i:s')."'";
            $sql = "INSERT INTO
                `vs_config`
            SET
                {$sql}
            ON DUPLICATE KEY UPDATE
                {$sql}";
            L::sql($sql);
            if(L::sql(true)){
                $subject = 'VS_'.$_POST['type'];
                $key = md5($subject);
                of_base_com_kv::del($key);
                return array (
                    "state" => 200,
                    "info" => "编辑成功!"
                );
            }else{
                return array (
                    "state" => 300,
                    "info" => "编辑失败!"
                );
            }
        }else{
            return array (
                "state" => 400,
                "info" => "重要信息不全"
            );
        }
    }

    /**
     * 描述：刷新缓存
     * 作者：Zoro.Zhu
     **/
    public function refreshCache(){
        $config = L::sql("SELECT
            `type`
        FROM
            `vs_config`");
        foreach($config as $row){
            $subject = 'VS_' . $row['type'];
            $key = md5($subject);
            of_base_com_kv::del($key);
        }
        return array('state' => 200, 'info' => '刷新成功');
    }

    /**
     * 描述：导入配置
     * 作者：Zoro.Zhu
    **/
    public function importCsv(){
        if(!empty($_POST['fileUrl'])){
            $path = ROOT_DIR . '/data/upload' . $_POST['fileUrl'];
            $i = 1;
            $info = array();
            while( $data = &of_base_com_csv::parse($path) ) {
                if($i === 1){
                    $i++;
                    continue;
                }
                $info[$data[0]] = $data[1];
            }
            return array('state' => 200, 'info' => $info);
        }
    }

    /**
     * 描述：导出配置
     * 作者：Zoro.Zhu
    **/
    public function exportCsv(){
        if(!empty($_GET['type'])){
            $info = L::sql("SELECT
                `type`, `data`, `remark` 
            FROM
                `vs_config` 
            WHERE
                `type` = '{$_GET['type']}'");
            of_base_com_csv::download($info[0]['remark']);
            of_base_com_csv::download(array('键', '值'));
            $data = json_decode($info[0]['data'], true);
            foreach ($data as $key => $value){
                of_base_com_csv::download(array($key, $value));
            }
        }
    }

}

return true;